//
// Created by (cie_tea)15694 on 2024/5/23.
//
#ifndef EVENTCONTROLLER_H
#define EVENTCONTROLLER_H

#include <unordered_map>
#include <json/json.h>
#include "IoBuffer.h"
#include "./ByteStreamHandler.h"
#include "../session/Session.h"
#include "../logger/Logger.h"
#define READ_BUF_SIZE 1024
#include <sys/epoll.h>

// using json = nlohmann::json;


// 连接状态结构体
//typedef struct {
//    int fd; // 文件描述符
//    char read_buf[READ_BUF_SIZE]; // 读取缓冲区
//    ssize_t buf_len; // 缓冲区当前的数据长度
//    IoBuffer *ioBuffer; // 读取缓冲区v2
//} connection_state;


class EventController {

    std::unordered_map<int, Session*> session_map;
    struct epoll_event ev;

public:
    int server_fd, client_fd, epoll_fd;

public:
    EventController();
    ~EventController();

    Session* get_connection_state(int fd);

    Session* handleNewConnection(int fd);
    int handleCloseConnection(int fd);
    void handleRecvEvent(int fd);
    void handleRecvEvent(int fd,std::string str);
    void handleRecvEvent(int fd,Json::Value* root, std::string str);
    void handleSendEvent(int fd);
    void handleSendEvent(int fd,const Json::Value& value);
    void handleSendEvent(int fd,const Json::Value* JsonValue, std::string str);
    int CheckSendIoBuffer(int fd);
    void handleErrorEvent(int fd);
    void handleErrorEvent(int fd, std::string str);
//    void addConnection(Session *session);
//    void removeConnection(Session *session);

private:
//    std::unordered_map<int, IoBuffer*> connection_buffers;


    void addConnection(int fd);
    void removeConnection(int fd);
};

#endif // EVENTCONTROLLER_H
